#include"SList.h"

SList* BuySListNode(SData x)
{
	SList* newnode = (SList*)malloc(sizeof(SList));
	if (newnode == NULL)
	{
		perror("malloc failed");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}

void SListPrint(SList* plist)
{
	assert(plist);
	SList* s = plist;
	while (s != NULL)
	{
		printf("%d->", s->data);
		s = s->next;
	}
	printf("\n");
}

void SListPushBack(SList** pplist, SData x)
{
	assert(pplist);
	if (*pplist == NULL)
	{
		*pplist = BuySListNode(x);
	}
	else
	{
		SList* temp = *pplist;
		while (temp->next)
		{
			temp = temp->next;
		}
		temp->next = BuySListNode(x);
	}
}

void SListPushFront(SList** pplist, SData x)
{
	assert(pplist);
	if (*pplist == NULL)
	{
		*pplist = BuySListNode(x);
	}
	else
	{
		SList* newnode = BuySListNode(x);
		newnode->next = *pplist;
		*pplist = newnode;
	}
}

void SListPopFront(SList** pplist)
{
	assert(pplist);
	assert(*pplist);
	SList* temp = (*pplist)->next;
	free(*pplist);
	*pplist = temp;
}

void SListPopBack(SList** pplist)
{
	assert(pplist);
	assert(*pplist);
	if ((*pplist)->next == NULL)
	{
		free(*pplist);
		*pplist = NULL;
	}
	else
	{
		SList* temp = *pplist;
		while (temp->next->next)
		{
			temp = temp->next;
		}
		free(temp->next);
		temp->next = NULL;
	}
}

SList* SListFind(SList* plist,SData x)
{
	assert(plist);
	SList* temp = plist;
	while (temp)
	{
		if (temp->data == x)
			return temp;
		temp = temp->next;
	}
	return NULL;
}

void SListInsertAfter(SList* pos, SData x)
{
	assert(pos);
	SList* newnode = BuySListNode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}

void SListEraseAfter(SList* pos)
{
	assert(pos);
	assert(pos->next);
	SList* temp = pos->next->next;
	free(pos->next);
	pos->next = temp;
}

void SListDestory(SList** plist)
{
	while (*plist)
	{
		SList* temp = (*plist)->next;
		free(*plist);
		*plist = temp;
	}
}


LTNode* BuyNode(SData x)
{
	LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
	if (newnode == NULL)
	{
		perror("malloc failed");
		exit(-1);
	}
	newnode->LTData = x;
	newnode->next = newnode;
	newnode->prev = newnode;
}

LTNode* LTInit()
{
	LTNode* newnode = BuyNode(0);
	newnode->next = newnode;
	newnode->prev = newnode;
	return newnode;
}

void LTPushBack(LTNode* phead, SData x)
{
	assert(phead);
	LTNode* tail = phead->prev;
	LTNode* newnode = BuyNode(x);
	newnode->prev = tail;
	tail->next = newnode;
	newnode->next = phead;
	phead->prev = newnode;
}

void LTPushFront(LTNode* phead, SData x)
{
	assert(phead);
	LTNode* tail = phead->next;
	LTNode* newnode = BuyNode(x);
	newnode->next = tail;
	tail->prev = newnode;
	newnode->prev = phead;
	phead->next = newnode;
}

void LTPopBack(LTNode* phead)
{
	assert(phead);
	assert(phead != phead->next);
	LTNode* temp = phead->prev->prev;
	free(phead->prev);
	phead->prev = temp;
	temp->next = phead;
}

void LTPopFront(LTNode* phead)
{
	assert(phead);
	assert(phead != phead->next);
	LTNode* temp = phead->next->next;
	free(phead->next);
	phead->next = temp;
	temp->prev = phead;
}

int LTSize(LTNode* phead)
{
	assert(phead);
	LTNode* temp = phead->next;
	int n = 0;
	while (temp != phead)
	{
		n++;
		temp = temp->next;
	}
	return n;
}

LTNode* LTFind(LTNode* phead, SData x)
{
	assert(phead);
	LTNode* temp = phead->next;
	while (temp != phead)
	{
		if (temp->LTData == x)
			return temp;
		temp = temp->next;
	}
	return NULL;
}

void LTInsert(LTNode* pos, SData x)
{
	assert(pos);
	LTNode* newnode = BuyNode(x);
	newnode->next = pos;
	newnode->prev = pos->prev;
	pos->prev->next = newnode;
	pos->prev = newnode;
}

void LTErase(LTNode* pos)
{
	assert(pos);
	pos->next->prev = pos->prev;
	pos->prev->next = pos->next;
	free(pos);
}

void LTPrint(LTNode* phead)
{
	assert(phead);
	LTNode* temp = phead->next;
	while (temp != phead)
	{
		printf("%d<->", temp->LTData);
		temp=temp->next;
	}
	printf("\n");
}










